package org.adl.registry.website;


import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.adl.registry.website.lucene.AdvancedQuery;

/**
 * Provides methods for preprocessing and relaying to RIM a search query submitted via the ADL Registry website Advanced Search form.
 * @author Jacob Marks - Joint ADL Co-Lab
 * @version 1.0
 * @license This software is licensed as open source software pursuant to the terms defined in the Joint ADL Co-Lab Open Source Software License.  Use of any part of this software constitutes acceptance of this license. The Joint ADL Co-Lab open source agreement is available at {@link http://adlregistry.adlnet.gov/source_license.html}.
 */
public class AdvancedSearchRequest extends SearchRequestHandler
{   
    /**
     * Instantiates a new instance of the <CODE>AdvancedSearchHandler</CODE> class.
     * @param request The <CODE>ServletRequest</CODE> generated by the Servlet receiving the submission of a Form based ADL Registry request.
     */
    public AdvancedSearchRequest(HttpServletRequest iRequest, ServletContext iContext)
    throws InvalidRequestException
    {
        super();
        
        super.setServletRequest(iRequest);
        super.setServletContext(iContext);
        
        String registry = iRequest.getParameter("registry"); // This parameter value should be the parameter containing the target registry.
        super.setRegistry(registry);
        
        String rimUrl = PortalUtil.getRimUrl(iContext, registry);
        super.setRimUrl(rimUrl);
        
        String cak = PortalUtil.getClientAccessKey(iContext);
        super.setClientAccessKey(cak);
    }

    /**
     * Initiates and carries out a communication session with RIM Lite.
     * @return Returns the <CODE>String</CODE> response from a request to RIM Lite. Generally these responses are always XML.
     * @throws java.io.IOException Throws <CODE>IOException</CODE> if communication with RIM Lite fails.
     */
    public String InvokeRequest()
    throws IOException, InvalidRequestException
    {
        String rimResponse = "";
        
        HttpServletRequest request = super.getServletRequest();
        
        String formattedQuery = "";
        
        // TODO: Eliminate the need for this.
        String form = request.getParameter("form"); // This parameter value should be the parameter containing an indicator of which form origionated the request.
        
        // Determine which form submitted the request.
        if (form.equalsIgnoreCase("advanced"))
        {
            //TODO: parse for complexity
            
            AdvancedQuery query = new AdvancedQuery();
            
            String u = request.getParameter("all_words_textbox");
            if ((u != null) && (u.length() > 0))
            {
                query.addAllTermQueryElement(u);
            }
            
            String u1 = request.getParameter("any_words_textbox");
            if ((u1 != null) && (u1.length() > 0))
            {
                query.addAnyTermQueryElement(u1);
            }
            
            String u2 = request.getParameter("phrase_textbox");
            if ((u2 != null) && (u2.length() > 0))
            {
                query.addPhraseQueryElement(u2);
            }
            
            String u3 = request.getParameter("exclude_words_textbox");
            if ((u3 != null) && (u3.length() > 0))
            {
                query.addExcludedTermsQueryElement(u3);
            }
            
            String u4 = request.getParameter("repository_textbox");
            if ((u4 != null) && (u4.length() > 0))
            {
                query.addRepositoryQueryElement(u4);
            }
            
            String u5 = request.getParameter("role_select");
            String u6 = request.getParameter("contributor_textbox");
            if ((u6 != null) && (u6.length() > 0))
            {
                query.addContributorQueryElement(u5, u6);
            }
            
            String u7 = request.getParameter("status_select");
            if ((u7 != null) && (u7.length() > 0))
            {
                query.addContentStatusQueryElement(u7);
            }
            
            //String u8 = request.getParameter("lstSearchFields");
            //if ((u8 != null) && (u8.length() > 0))
            //{
            //    query.addLimitingField(u8);
            //}
            
            super.setQuery(query.toString());
            
            // Make search request.
            rimResponse = super.sendSearchRequest();
        }
        else if (form.equalsIgnoreCase("contentid"))
        {
            //TODO: parse for complexity
            
            String u = request.getParameter("content_identifier_textbox");
            
            AdvancedQuery query = new AdvancedQuery();
            
            query.addContentIdentifierQueryElement(u);
            
            super.setQuery(query.toString());
            
            // Make search request.
            rimResponse = super.sendSearchRequest();
        }
        else if (form.equalsIgnoreCase("metadataid"))
        {
            //TODO: parse for complexity
           
            String u = request.getParameter("metadata_identifier_textbox");
            
            AdvancedQuery query = new AdvancedQuery();
            
            query.addMetadataIdentifierQueryElement(u);
            
            super.setQuery(query.toString());
            
            // Make search request.
            rimResponse = super.sendSearchRequest();
        }
        else
        {
            throw new InvalidRequestException("Unable to process request. Unsupported user interface identified in request.");
        }
        
        return rimResponse;
    }
}
